package {
	import flash.display.BitmapData;
    import org.flixel.*;
  
    // Overall level terrain
    public class DisappearingPlatform extends FlxSprite {

        private var _Id: Number;
        
        // Constructor to create a platform (rectangle)
        public function DisappearingPlatform(X:Number, Y:Number, Width:Number, Height:Number, Id:Number, Vis:Boolean) {
            // Create sprite at coordinate X, Y
            super(X, Y);

            // bounding box
            width = Width;
            height = Height;

            immovable = true;
			solid = Vis;

            _Id = Id;
            
            createGraphic();
        }
		
		// Change opacity if on/off
		override public function update():void {
			if (solid) {
				alpha = 1;
			} else {
				alpha = 0.2;
			}
			
			super.update();
		}
        
        public function getId():Number {
            return _Id;
        }
		
		public function toggleVisible():void {
			solid = !solid;
			if ( solid )
				createGraphic();
		}
		
		public function isVisible():Boolean {
			return visible;
		}
		
		private function createGraphic():void {
			pixels = new BitmapData(width, height, true, 0x00000000);
			framePixels = pixels;
			// Manipulate the corners
			for ( var i:int = 0; i < width; i++ )
			{
				for ( var j:int = 0; j < height; j++ )
				{
					// Generate color (we want some patterns to get a smeary effect)
					/*var Color:uint = (Math.random() * 16 + 4 * i) % 32;
					Color = Color << 8;
					Color = Color & 0x0000FF00;
					Color += (Math.random() * 16 + j) % 48;
					Color = Color << 8;
					Color = Color & 0x00FFFF00;
					Color += (Math.random() * 16 + i * j) % 64;
					Color &= 0x00FFFFFF;*/
					var Color:uint = 0xFF888888;
						
					// Detect top-left
					if ( i < 3 && j < 3 )
					{
						var dist:Number = (i + j) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i >= width-3 && j < 3 ) // Top-right
					{
						var iOff:int = width - i - 1;
						var dist:Number = (iOff + j) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i < 3 && j >= height-3 ) // Bottom-left
					{
						var jOff:int = height - j - 1;
						var dist:Number = (i + jOff) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i >= width-3 && j >= height-3 ) // Bottom-left
					{
						var jOff:int = height - j - 1;
						var iOff:int = width - i - 1;
						var dist:Number = (iOff + jOff) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i < 3 )			// Left side
					{
						var dist:Number = i * i / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( j < 3 )			// Top side
					{
						var dist:Number = j * j / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( j >= height-3 )			// Bottom side
					{
						var jOff:int = height - j - 1;
						var dist:Number = jOff * jOff / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i >= width-3 )			// Right side
					{
						var iOff:int = width - i - 1;
						var dist:Number = iOff * iOff / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else
					{
						Color |= 0xFF000000;
						super.pixels.setPixel32(i, j, Color);
					}
				}
			}
		}
    }
}